package com.augurit.smartform.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.smartform.entity.SmPage;

/**
 * 智能表单页面DAO类
 */
@Repository
public class SmPageDao extends BaseDao<SmPage,Long>{
	
    public void updateEntity(SmPage entity) {
        Session session=this.getSession();
        session.flush();
        session.clear();
        session.update(entity);
    }
	
	/**
	 * 查询相同页面名称的数量
	 * @param name
	 * @return
	 * cuigeqiang
	 */
	public long countByName(String name){
		String hql="from SmPage t where t.name=?";
		return this.countHqlResult(hql, name);
	}
	
	/**
	 * 通过PAGECODE查询唯一的SMPAGE
	 * @param pageCode
	 * @return
	 * cuigeqiang
	 */
	public SmPage getSmPageByPageCode(String pageCode){
		String hql="from SmPage where pageCode=:pageCode";
		return (SmPage)this.getSession().createQuery(hql).setString("pageCode", pageCode).uniqueResult();
	}
	
	/**
	 * 通过PAGEID查询唯一的SMPAGE
	 * @param pageId
	 * @return
	 * cuigeqiang
	 */
	public SmPage getSmPageByPageId(String pageId){
		String hql="from SmPage where pageId=:pageId";
		return (SmPage)this.getSession().createQuery(hql).setString("pageId", pageId).uniqueResult();
	}
	
	/**
	 * 通过模板ID加载智能表单数据
	 * @param templateId
	 * @return
	 */
	public List<Map> getSourceDataByTemplateId(String templateId){
//		String hql="select sourceXml as xml, sourceHtml as html, sourceFtl as ftl,clientJavascript as clientJs, serverJavascript as serverJs from SmPage where templateId = "+templateId;
		String hql="select sourceXml as xml, sourceHtml as html, sourceFtl as ftl,clientJavascript as clientJs, serverJavascript as serverJs from SmPage where templateId = "+templateId +" order by to_number(substr(pageId,5))";
		return this.getSession().createQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
	}
	
	/**
	 * 得到没有目录的页面
	 * @return
	 */
	public List<SmPage> getNoDirSmPages(){
		String hql="from SmPage where pageDirId is null";
		return this.find(hql, new Object[]{});
	}
	
	/**
	 * 通过dirId查询SMPAGE列表
	 * @param pageId
	 * @return
	 * cuigeqiang
	 */
	public List<SmPage> getSmPageByDirId(Long dirId){
		String hql="from SmPage where pageDirId=:pageDirId";
		return this.getSession().createQuery(hql).setLong("pageDirId", dirId).list();
	}
	
	/**
	 * 更新目录ID
	 * @param smPageId
	 * @param dirId
	 * cuigeqiang
	 */
	public void updateDirId(Long smPageId,Long dirId){
		Query query=null;
		if(dirId!=null){
			String hql="update SmPage set pageDirId=:pageDirId where id=:id";
			query=this.getSession().createQuery(hql).setLong("pageDirId", dirId).setLong("id", smPageId);
		}else{
			String hql="update SmPage set pageDirId=null where id=:id";
			query=this.getSession().createQuery(hql).setLong("id", smPageId);
		}
		query.executeUpdate();
	}
	
	/**
	 * 清空目录ID
	 * @param smPageId
	 * @param dirId
	 * cuigeqiang
	 */
	public void clearDirId(Long dirId){
		Query query=null;
		String hql="update SmPage set pageDirId=null where pageDirId=:pageDirId";
		query=this.getSession().createQuery(hql).setLong("pageDirId", dirId);
		query.executeUpdate();
	}
}
